﻿@{
    ViewBag.Title = "基础信息管理";
    ViewBag.First = "基础信息管理";
    ViewBag.Second = "基础信息列表";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
    var baseItemType = ViewBag.BaseItemType as IList<Sc.Domain.BaseItemType>;
}
<script type="text/javascript">
    $(document).ready(function (e) {
        $("#typeName").uedSelect({
            width: 110
        });
        $("#selType").uedSelect({
            width: 247
        });
        loadData();
    });
    //获取
    function loadData() {
        var url = '@Url.Action("GetBaseItem")';
        var load = loadTip("数据加载中，请稍候", 16, 0);
        $.get(url, { baseItemTypeId: $("#typeName").val() }, function (data) {
            var table = document.getElementById("listData");
            $("#listData").find("tr").remove();
            $.each(data, function (index, n) {
                var row = table.insertRow(table.rows.length);
                row.Id = n.Id;
                var cb = row.insertCell(0);
                cb.innerHTML = "<input type='checkbox' tag=" + n.Id + " />";
                var name = row.insertCell(1);
                name.innerHTML = n.Name;
                var bName = row.insertCell(2);
                $(bName).attr("bid", n.BaseItemTypeId);
                bName.innerHTML = n.BaseItemTypeName;
                var remark = row.insertCell(3);
                remark.innerHTML = n.Remark;
            });
            $('.imgtable tbody tr:odd').addClass('odd');
            layer.close(load);
        });
    }
    //新增
    function add(title) {
        layer.open({
            title: title,
            type: 1,
            area: ["700px", "400px"],
            content: $("#baseItem")
        });
    }
    //修改分类
    function edit() {
        var tmp = $("#listData").find("input[type=checkbox]:checked");
        if (tmp.length <= 0) {
            loadTip("请至少选择一条数据修改", 2, 1000);
            return false;
        }
        if (tmp.length >1) {
            loadTip("只能选择一条数据修改", 2, 1000);
            return false;
        }
        var first = $(tmp[0]).parent();
        $("#txtName").val(first.next().text());
        $("#selType").val(first.next().next().attr("bid"));
        $("#busId").val($(tmp[0]).attr("tag"));
        $("#selType").prev().find("em[class=uew-select-text]").text(first.next().next().text());
        $("#txtRemark").val(first.next().next().next().text());
        add("修改基本信息");
    }
    //保存新增数据
    function saveData(tag) {
        var txtName = $("#txtName").val();
        var selType = $("#selType").val();
        var txtRemark = $("#txtRemark").val();
        if (txtName.length <= 0) {
            Tips("分类名称不能为空", "txtName");
            return false;
        }
        if (selType == 0) {
            Tips("请选择所属大类", "selType");
            return false;
        }
        var url = "@Url.Action("SaveBaseItem")";
        var load = loadTip("数据提交中，请稍候", 16, 0);
        var ent = new Object();
        ent.Name = txtName; ent.BaseItemTypeId = selType; ent.Remark = txtRemark; ent.Id = $("#busId").val();
        $.post(url, { data: JSON.stringify(ent) }, function (data) {
            if (data > 0) {
                $("#busId").val(0);
                if (tag == 1) {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        layer.closeAll();
                    });
                } else {
                    layer.msg("数据保存成功", { icon: 1, time: 1000, shade: [0.3, '#000'] });
                }
                $("#txtName").val(""); $("#selType").val(0); $("#txtRemark").val("");
                loadData();
            }
        });
    }
    //删除分类
    function del() {
        layer.confirm("您确定要删除吗？", function () {
            var tmp = $("#listData").find("input[type=checkbox]:checked");
            if (tmp.length <= 0) {
                loadTip("请至少选择一条数据删除", 2, 1000);
                return false;
            }
            var url = "@Url.Action("DeleteList")";
            var ids = "";
            for (var i = 0; i < tmp.length; i++) {
                if (i > 0) {
                    ids += ",";
                }
                ids += $(tmp[i]).attr("tag");
            }
            var load = loadTip("数据提交中，请稍候", 16, 0);
            $.post(url, { ids: ids }, function (data) {
                if (data > 0) {
                    layer.msg("数据删除成功", { icon: 1, time: 1000, shade: [0.3, '#000'] }, function () {
                        loadData();
                    });
                }
            });
        });
    }
    //按分类查询
    function ch() {
        loadData();
    }
</script>
<div class="rightinfo">
    <div class="tools">
        <ul class="toolbar">
            <li class="click" onclick="add('新增基本信息');"><span><img src="~/Content/images/t01.png" /></span>添加</li>
            <li class="click" onclick="edit();"><span><img src="~/Content/images/t02.png" /></span>修改</li>
            <li onclick="del();"><span><img src="~/Content/images/t03.png" /></span>删除</li>
        </ul>
        <input type="hidden" id="busId" value="0" />
        <ul class="seachform" style="float:right;">
            <li>
                <label>类型</label>
                <div class="vocation">
                    <select name="select" class="select2" id="typeName" onchange="ch()">
                        <option value="0">----请选择----</option>
                        @if (baseItemType != null)
                        {
                            foreach (var item in baseItemType)
                            {
                                <option value="@item.Id">@item.Name</option>
                            }
                        }
                    </select>
                </div>
            </li>
        </ul>
    </div>
    <table class="tablelist">
        <thead>
            <tr>
                <th width="30px"></th>
                <th>名称</th>
                <th>所属分类</th>
                <th>说明</th>
            </tr>
        </thead>
        <tbody id="listData"></tbody>
    </table>
</div>
<!--分类添加-->
<div id="baseItem" style="display:none;">
    <table style="width:90%;margin-left:20px;line-height:48px;">
        <tr>
            <td><label>分类名称</label></td>
            <td><input id="txtName" type="text" class="dfinput" /></td>
        </tr>
        <tr>
            <td><label>所属分类</label></td>
            <td>
                <select name="select" class="select2"  id="selType">
                    <option value="0">----请选择----</option>
                    @if (baseItemType != null)
                    {
                        foreach (var item in baseItemType)
                        {
                            <option value="@item.Id">@item.Name</option>
                        }
                    }
                </select>
            </td>
        </tr>
        <tr>
            <td><label>分类描述</label></td>
            <td><textarea id="txtRemark" class="textinput"></textarea></td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input onclick="saveData(1);" type="button" class="btn" value="保存退出" />
                <input style="margin-left:10px" onclick="saveData(2);" type="button" class="btn" value="保存继续" />
            </td>
        </tr>
    </table>
</div>

